<?php
namespace app\agency\controller;
use think\Db;
use clt\Tree;
use think\Request;
use think\Controller;
class Order extends Common
{
    public function _initialize(){
        parent::_initialize();
    }
    //列表页
    public function index(){
        $agency_id = session('agency_id');
        if(request()->isPost()) {
            $key = input('post.key');
            $this->assign('testkey', $key);
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list = Db::name('order')
                ->where('agency_id',$agency_id)
                ->where('order_sn', 'like', "%" . $key . "%")
                ->order('addtime DESC')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:s',$v['addtime']);
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }

    
    //数据添加
    public function add(){
        if(request()->isPost()) {
            //构建数组
            $data = input('post.');

            $data['addtime'] = time();    //添加时间
            $data['status'] = 0;           //状态,默认待审核
            $data['agency_id'] = session('agency_id');  //机构ID
            $data['addtime'] = time();     //添加时间
            $data['course_sn'] = 'kc_'.date('YmdHis',time()).rand_string('3',1);     //课程编号



            //通过提交过的栏目ID,获取其家族路径
            $parent_id_path = db('category')->where('cate_id',$data['cat_id'])->value('parent_id_path');
            $path_arr = explode(',',$parent_id_path);

            if(is_array($path_arr)){
                if($path_arr[1] > 0 ){
                    $data['cat_id1'] = $path_arr[1];   //一级类别id
                }

                if($path_arr[2] > 0 ){
                    $data['cat_id2'] = $path_arr[2];   //二级类别id
                }

                if($path_arr[3] > 0 ){
                    $data['cat_id3'] = $path_arr[3];    //三级类别id
                }
            }
            $row =  db('order')->insert($data);     //添加数据
            $userId = db('order')->getLastInsID();

            if($row > 0 ){
                $result['code'] = 1;
                $result['msg'] = '课程添加成功,等待后台审核';
                $result['url'] = url('edit',array('order_id'=>$userId));
            }else{
                $result['code'] = 0;
                $result['msg'] = '操作失败';
                $result['url'] = url('add');
            }

            return $result;
        }else{


            //$this->assign('adtypeList',json_encode($adtypeList,true));

            $this->assign('title','添加课程');
            $this->assign('info','null');
            return $this->fetch('form');
        }
    }

    //数据修改
    public function edit(){
        if(request()->isPost()) {
            $data = input('post.');

            //通过提交过的栏目ID,获取其家族路径
            $parent_id_path = db('category')->where('cate_id',$data['cat_id'])->value('parent_id_path');
            $path_arr = explode(',',$parent_id_path);

            if(is_array($path_arr)) {
                if ($path_arr[1] > 0) {
                    $data['cat_id1'] = $path_arr[1];   //一级类别id
                }

                if ($path_arr[2] > 0) {
                    $data['cat_id2'] = $path_arr[2];   //二级类别id
                }

                if ($path_arr[3] > 0) {
                    $data['cat_id3'] = $path_arr[3];    //三级类别id
                }
            }
            $row = db('order')->update($data);

            $result['code'] = 1;
            $result['msg'] = '课程修改成功!';
            $result['url'] = url('index');
            return $result;
        }else{

            $order_id=input('order_id');
            //1.查询课程信息
            $adInfo=db('order')->where(array('order_id'=>$order_id))->find();

            //2.查询关联的栏目名称
            if($adInfo['cat_id3'] > 0){
                $catInfo = db('category')->field('cate_id,name')->where('cate_id',$adInfo['cat_id3'])->find();
            }else if($adInfo['cat_id2'] > 0){
                $catInfo = db('category')->field('cate_id,name')->where('cate_id',$adInfo['cat_id2'])->find();
            }else if($adInfo['cat_id1'] > 0){
                $catInfo = db('category')->field('cate_id,name')->where('cate_id',$adInfo['cat_id1'])->find();
            }

            $this->assign('info',json_encode($adInfo,true));
            $this->assign('catInfo',$catInfo);
            $this->assign('title',lang('edit').'课程');
            $this->assign('order_id',$order_id);
            return $this->fetch('form');
        }
    }

    //修改状态
    public function editState(){
        $id=input('post.id');
        $open=db('order')->where(array('order_id'=>$id))->value('open');//判断当前状态情况
        if($open==1){
            $data['open'] = 0;
            db('order')->where(array('order_id'=>$id))->setField($data);
            $data['status'] = 1;
        }else{
            $data['open'] = 1;
            db('order')->where(array('order_id'=>$id))->setField($data);
            $data['status'] = 1;
        }
        return $data;
    }

   
    
    //单条数据删除
    public function del(){
        db('order')->where(array('order_id'=>input('order_id')))->delete();
        return ['code'=>1,'msg'=>'删除成功！'];
    }

    //数据批量删除
    public function delall(){
        $map['order_id'] =array('in',input('param.ids/a'));
        db('order')->where($map)->delete();
        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        $result['url'] = url('index');
        return $result;
    }
}